<?php

namespace App\Http\Controllers;

use App\models\Goods;
use App\models\Order;
use Illuminate\Http\Request;
//测试一下
class ShopController extends Controller
{
    // 首页
    public function index()
    {
        $list = Order::all();
        return view('shop.index',['list'=>$list]);
    }
    //逛一逛
    public function create()
    {
        $list = Goods::all();
        return view('shop.add_goods',['list'=>$list]);
    }
    //处理ajax传过来的值；
    public function store(Request $request)
    {
        $id = $request->input('id');
        $gname = $request->input('gname');
        $order_num = $request->input('nums');
        $total_prices = $request->input('total_money');
        $data = [
            'gname' => $gname,
            'order_num' => $order_num,
            'total_prices' => $total_prices,
        ];
        $order = new Order();
        if($order->upGoods($id,$data)){
            echo json_encode(array('res'=>1));
        }else{
            echo json_encode(array('res'=>2));
        }

    }
    //购买商品的方法
    public function show($id)
    {
        $goods_msg = Goods::where('status',1)->find($id);
        if($goods_msg){
            $data = [
                'gname' => $goods_msg->gname,
                'gprice' => $goods_msg->gprice,
                'order_time' => date('Y-m-d H:i:s'),
                'order_num' => 1,
                'total_prices' => $goods_msg->gprice
            ];
            $order = new Order();
            $b = Order::where('gname','=',$goods_msg->gname)->count();
            if($b==0){
                $order->insertGetId($data);
                $url = url('shop/create');
                echo "<script>alert('添加成功！');window.location.href='{$url}';</script>";die;
            }else{
                $url = url('shop/create');
                echo "<script>alert('订单已存在');window.location.href='{$url}';</script>";die;
            }

        }
    }
    //在这边执行删除的方法
    public function edit($id)
    {
        if(Order::query()->where('id',$id)->delete()){
            $url = url('/');
            echo "<script>alert('删除成功！');window.location.href='{$url}'</script>";die;
        }else{
            $url = url('/');
            echo "<script>alert('删除失败！');window.location.href='{$url}'</script>";die;
        }

    }
    //支付功能
    public function update(Request $request, $id)
    {
        $id = explode(',',rtrim($id,','));
        $order = new Order();
        $goods = new Goods();
        foreach($id as $v){
            $order_num = $order->getfind($v)['order_num'];
            $goods_list = Goods::query()->where('gname',$order->getfind($v)['gname'])->first();
            if($order_num>$goods_list->gstore){
                echo json_encode(array('res'=>false,'msg'=>$goods_list->gname."库存不足！"));die;
            }else{
                $data = ['gstore'=>($goods_list->gstore-$order_num)];
                $goods->upGoods($goods_list->id,$data);
            }
        }
        Order::query()->delete();
        echo json_encode(array('res'=>true));die;

    }
    public function destroy($id)
    {

    }
}
